﻿using System;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.processor.type.comm;
using SharpSqlAnalyzer.pp.stmtformatter;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;

namespace SharpSqlAnalyzer.pp.processor.type.merge
{
	// Token: 0x0200023A RID: 570
	public class AppendLineAfterMergeTableNameProcessor : AbstractProcessor
	{
		// Token: 0x060012E3 RID: 4835 RVA: 0x0031B5F4 File Offset: 0x003197F4
		public override void process(TParseTreeNode nodeArg)
		{
			TMergeSqlStatement sql = nodeArg as TMergeSqlStatement;
			this.processMergeTable(sql);
			this.processOnCondtion(sql);
		}

		// Token: 0x060012E4 RID: 4836 RVA: 0x0031B618 File Offset: 0x00319818
		private void processOnCondtion(TMergeSqlStatement sql)
		{
			TSourceToken tsourceToken = SourceTokenSearcher.forwardSearch(sql.UsingTable.endToken, 5, "on");
			SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken);
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
			int num = SourceTokenOperator.curColumnNumberVT(this.getMergeToken(sql));
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(num));
			if (sql.Condition != null)
			{
				TSourceToken tsourceToken2 = SourceTokenSearcher.forwardSearch(tsourceToken, 3, "(");
				num = SourceTokenOperator.curColumnNumberVT(sql.TargetTable.startToken);
				int num2 = SourceTokenOperator.curColumnNumberVT(tsourceToken);
				if (tsourceToken2 != null)
				{
					SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken2);
					SourceTokenOperator.addBefore(this.Option, tsourceToken2, SourceTokenOperator.createWhitespaceSourceToken(num - num2 - tsourceToken.ToString().Length));
				}
			}
		}

		// Token: 0x060012E5 RID: 4837 RVA: 0x0031B6D8 File Offset: 0x003198D8
		private void processMergeTable(TMergeSqlStatement sql)
		{
			int length = SourceTokenOperator.curColumnNumberVT(sql.TargetTable.startToken);
			TSourceToken tsourceToken = SourceTokenSearcher.backforwardSearch(sql.UsingTable.startToken, 5, "using");
			if (tsourceToken != null)
			{
				SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken);
				SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
				SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(length));
				if (sql.UsingTable.Subquery != null)
				{
					FormatterFactory.processStatement(this.Option, sql.UsingTable.Subquery);
				}
			}
		}

		// Token: 0x060012E6 RID: 4838 RVA: 0x0031B764 File Offset: 0x00319964
		private TSourceToken getMergeToken(TMergeSqlStatement node)
		{
			TSourceTokenList container = node.startToken.container;
			for (int i = 0; i < container.size(); i++)
			{
				TSourceToken tsourceToken = container.get(i);
				if (tsourceToken.ToString().Equals("merge", StringComparison.CurrentCultureIgnoreCase))
				{
					return tsourceToken;
				}
			}
			return null;
		}
	}
}
